package com.epsi.mightymaps.hdl;

import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import android.util.Log;

import com.epsi.mightymaps.mdl.Parcours;
import com.epsi.mightymaps.mdl.PointDePassage;

public class ParcoursContentHandler implements ContentHandler {

	private static final String XML_ATTRIBUTE_ADDRESS = "adresse";
	private static final String XML_ATTRIBUTE_DESC = "description";
	private static final String XML_ATTRIBUTE_TITLE = "titre";
	private static final String XML_ATTRIBUTE_LNG = "lng";
	private static final String XML_ATTRIBUTE_LAT = "lat";
	private static final String XML_ATTRIBUTE_ID = "id";
	private static final String XML_TAG_PT = "point";
	private static final String XML_TAG_PARC = "parcours";

	private final List<Parcours> listeparcours;
	private Parcours parcours;

	public ParcoursContentHandler(List<Parcours> liste) {
		this.listeparcours = liste;
	}

	public void startDocument() throws SAXException {
		parcours = new Parcours();
		Log.d("PARSE", "START");
	}

	public void startElement(final String pUri, final String pLocalName,
			final String pQName, final Attributes pAtts) throws SAXException {
		Log.d("PARSE", pLocalName);
		if (pLocalName.equals(XML_TAG_PT)) {
			int id = Integer.parseInt(pAtts.getValue(XML_ATTRIBUTE_ID));
			double lat = Double.parseDouble(pAtts.getValue(XML_ATTRIBUTE_LAT));
			double lng = Double.parseDouble(pAtts.getValue(XML_ATTRIBUTE_LNG));
			String adresse = pAtts.getValue(XML_ATTRIBUTE_ADDRESS);
			String titre = pAtts.getValue(XML_ATTRIBUTE_TITLE);

			parcours.getPoints().add(
					new PointDePassage(id, titre, adresse, lat, lng));
			Log.d("PARCOURS NB POINTS", "" + parcours.getPoints().size());
		}
		if (pLocalName.equals(XML_TAG_PARC)) {
			String description = pAtts.getValue(XML_ATTRIBUTE_DESC);
			String titre = pAtts.getValue(XML_ATTRIBUTE_TITLE);
			parcours.setDescription(description);
			parcours.setTitre(titre);
			Log.d("PARCOURS ", "" + parcours.getTitre());
		}
	}

	public void characters(final char[] pCh, final int pStart, final int pLength)
			throws SAXException {

	}

	public void endDocument() throws SAXException {
		listeparcours.add(parcours);
	}

	public void endElement(final String pUri, final String pLocalName,
			final String pQName) throws SAXException {

	}

	public void endPrefixMapping(final String pPrefix) throws SAXException {

	}

	public void ignorableWhitespace(final char[] pCh, final int pStart,
			final int pLength) throws SAXException {
	}

	public void processingInstruction(final String pTarget, final String pData)
			throws SAXException {

	}

	public void setDocumentLocator(final Locator pLocator) {

	}

	public void skippedEntity(final String pName) throws SAXException {

	}

	public void startPrefixMapping(final String pPrefix, final String pUri)
			throws SAXException {

	}

}
